package com.example.template.app;

import android.os.Bundle;
import android.view.MenuItem;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.ViewPager;

import com.example.base.adapter.FragmentPagerAdapter;
import com.example.base.ui.BaseActivity;
import com.example.base.ui.BaseFragment;
import com.example.base.ui.viewbinding.DataBindingConfig;
import com.example.template.BR;
import com.example.template.R;
import com.example.template.databinding.ActivityMainBinding;
import com.example.template.ui.community.page.CommunityFragment;
import com.example.template.ui.home.page.HomeFragment;
import com.example.template.ui.market.page.MarketFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import java.util.Arrays;

public class AppActivity extends BaseActivity {

    private ActivityMainBinding binding;
    private String[] mTitles;

    @Override
    protected void initViewModel() {

    }

    @Override
    protected DataBindingConfig getDataBindingConfig() {
        return new DataBindingConfig(R.layout.activity_main, BR._all,null);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = (ActivityMainBinding) getBinding();
        mTitles = getResources().getStringArray(R.array.nav_home_titles);

        FragmentPagerAdapter<BaseFragment> mPagerAdapter = new FragmentPagerAdapter<>(this);
        mPagerAdapter.addFragment(new HomeFragment());
        mPagerAdapter.addFragment(new MarketFragment());
        mPagerAdapter.addFragment(new CommunityFragment());
        binding.viewPager.setAdapter(mPagerAdapter);

        initListeners();
    }

    protected void initListeners() {

        //主页事件监听
        binding.viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                MenuItem item = binding.bottomNavigation.getMenu().getItem(position);
                item.setChecked(true);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        binding.bottomNavigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                int index = Arrays.asList(mTitles).indexOf(item.getTitle());
                if (index != -1) {
                    binding.viewPager.setCurrentItem(index, false);
                    return true;
                }
                return false;
            }
        });
//        if (MMKVUtils.getInt("theme",R.style.AppTheme) == R.style.AppTheme){
//            binding.includeMain.bottomNavigation.setBackgroundColor(ResUtils.getColor(R.color.app_color_white));
//        } else {
//            binding.includeMain.bottomNavigation.setBackgroundColor(ResUtils.getColor(R.color.app_color_black));
//        }
    }


}